package com.ikai.ruderesponser;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;

/**
 * Servlet that handles the homepage.
 * 
 * @author Ikai Lan <ikai@google.com>
 * 
 */
@SuppressWarnings("serial")
public class HomeServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
	    throws IOException, ServletException {
	DatastoreService datastore = DatastoreServiceFactory
		.getDatastoreService();
	Query query = new Query("Rudeness");
	query.addSort("useCount", SortDirection.DESCENDING);
	List<Entity> rudenesses = datastore.prepare(query).asList(
		FetchOptions.Builder.withLimit(50));

	req.setAttribute("rudenesses", rudenesses);

	req.getRequestDispatcher("WEB-INF/home.jsp").forward(req, resp);
    }
}
